package interfacePackageStructures;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;

import mapPackage.IDTroncon;
import mapPackage.ITroncon;
import mapPackage.MapException;

import pathFinderPackage.Itineraire;
import xmlPackage.Carte;

public class MyTableEtapes extends AbstractTableModel {

	Icon iconSupprimer = new ImageIcon(getClass().getResource("/images/supprimer.gif"));

	private String[] columnNames = {"Etape",
                                    "Cumul",
                                    "Temps",
                                    "Route",
                                    "Indications"};
	
	ArrayList<Etape> data = new ArrayList<Etape>();

	public MyTableEtapes(Itineraire it) {
		data.clear();
		int m_numeroEtape = 1;
		ITroncon m_troncon;
		String v_courante = it.getVilleDepart();
		String v_suivante;
		float tempsCumul = 0;
		    Iterator<IDTroncon> iteratorTroncon = it.getTroncons().iterator();
		    
		    while (iteratorTroncon.hasNext()) {
				try {
					IDTroncon w_tronconCourant = iteratorTroncon.next();
					m_troncon = Carte.GetInstance().getTroncon(w_tronconCourant);
					v_suivante = m_troncon.getAutreVille(v_courante);
					float temps = (float)m_troncon.getLongueur() / ((float)m_troncon.getVitesseMax() / 360);	
					tempsCumul += temps;
					float tempsHeures = temps/3600;
					float tempsMinutes = temps/60 - 60 * tempsHeures;
					float tempsSecondes = temps%60 ;
	
					String tempsString = "";
					if ((int)tempsHeures != 0) {
						tempsString += (int) tempsHeures + "h";
					}
					if ((int)tempsMinutes != 0) {
						tempsString += (int) tempsMinutes + "min";
					}
					if ((int)tempsSecondes != 0) {
						tempsString += (int) tempsSecondes + "s";
					}
					
					String textTrajet = "Vous \u00eates \u00e0 " + v_courante + ". \n\n";
					textTrajet+= "Route " + m_troncon.getRoute();
					textTrajet+= " direction " + v_suivante + ".\n[" + m_troncon.getLongueur() + " km]\n\n";
					
					float tempsHeuresCumul = tempsCumul/3600;
					float tempsMinutesCumul = tempsCumul/60 - 60 * tempsHeuresCumul;
					float tempsSecondesCumul = tempsCumul%60 ;
	
					String tempsStringCumul = "";
					if ((int)tempsHeuresCumul != 0) {
						tempsStringCumul += (int) tempsHeuresCumul + "h";
					}
					if ((int)tempsMinutesCumul != 0) {
						tempsStringCumul += (int) tempsMinutesCumul + "min";
					}
					if ((int)tempsSecondesCumul != 0) {
						tempsStringCumul += (int) tempsSecondesCumul + "s";
					}

					data.add(new Etape(m_numeroEtape,tempsStringCumul,tempsString, m_troncon.getRoute(),textTrajet));
				v_courante = v_suivante;
				} catch (MapException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 m_numeroEtape++;	
			}

			fireTableDataChanged();
	}
	
	public ArrayList<Etape> getData() {
		return data;
	}
	
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    	if (col == 0) return data.get(row).getNumero();
    	if (col == 1) return data.get(row).getCumul();
    	if (col == 2) return data.get(row).getTemps();
    	if (col == 3) return data.get(row).getRoute();
    	if (col == 4) return data.get(row).getIndications();
    	else return "";
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
            return false;
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {           

    	//si la ligne existe pas on la cree sinon modif
    	if ((data.size() <= row) || (data.size() == 0 )){
    			data.add(new Etape((Integer)value));
    	}
    	else {
    		Etape w_etapeCour = data.get(row);
    		if (col == 0) w_etapeCour.setNumero((Integer)value);
    		else if (col == 1) {
    			w_etapeCour.setCumul((String)value);
    		}
    		else if (col == 2) {
    			w_etapeCour.setCumul((String)value);
    		}
    		else if (col == 3) {
    			w_etapeCour.setTemps((String)value);
    		}
    		else if (col == 3) {
    			w_etapeCour.setRoute((String)value);
    		}
    		else if (col == 4) {
    			w_etapeCour.setIndications((String)value);
    		}
        fireTableCellUpdated(row, col);
    	}
    }

    private void printDebugData() {
    }
    
}
